#!/bin/bash

#   Copyright 2012 Marcin Litwiński
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


# Universal Server Admin Tool v1.0
# by Marcin Litwiński
# marci.litwinski@gmail.com litwinski@white-paper-media.de
# Apache License 2.0

snapshot() {
snapshot_dir=$(dialog --stdout --title "Where is snapshot.tar" --fselect / 14 48)
}

yesno_snap() {
dialog --stdout --title "Question" --yesno "Are you sure $snapshot_dir?" 14 48
case $? in
 0)
	;;
 1)
	clear
	snapshot
	;;	 
esac
}

igd() {
gitorious_dir=$(dialog --stdout --title "Where is installed Gitorious?" --fselect / 14 48)
yesno 
}

yesno() {
dialog --stdout --title "Question" --yesno "Are you sure $gitorious_dir ?" 14 48
case $? in
 0)
	;;
 1)
	clear
	igd
	;;	 
esac
}

if [ $1 == igd ]
then
	igd
else
break
fi



cmd=(dialog --title "Gitorious Migration" --radiolist "Select options:" 22 76 16)
options=(1 "Backup current Gitorious database" off    # any option can be set to default to "on"
         2 "Restor Gitorious database" off
	 3 "Go to server package installation menu" off
	 4 "Exit" off)

scr_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)


#until [[$? != true]]; do
#	gitorious_dir=$(dialog --stdout --title "Where is installed Gitorious?" --fselect / 14 48);
#	dialog --stdout --title "Question" --yesno "Are you sure that Gitorious is in $gitorious_dir ?" 14 48;
#done

clear

for choice in $choices
do
    case $choice in
        1)
	    igd
	    yesno
	    echo '******************************'
	    echo '*     Gitorious Snapshot     *'
	    echo '******************************'
	    echo ''	
	    echo 'Preparing...'		    
            sleep 3
	    echo ''
	    if [ -e $gitorious_dir/script/snapshot ]; then
		    echo 'Runnig snapshot script'
		    sleep 3
		    chmod +x $gitorious_dir/script/snapshot && $gitorious_dir/script/snapshot
	    else
	      	    echo 'Copying snapshot script'
		    sleep 3
		    cp $scr_dir/gitorious_scripts/snapshot $gitorious_dir/script/ && chmod +x $gitorious_dir/script/snapshot && cp $scr_dir/gitorious_scripts/backup.rake $gitorious_dir/lib/task/ 
		    echo "Running restore script"
 		    sleep 3
		    $gitorious_dir/script/snapshot
		    
	    fi
	    break;;
        2)
	    igd
	    yesno
	    snapshot
	    yesno_snap
	    echo '******************************'
	    echo '*      Gitorious Backup      *'
	    echo '******************************'
	    echo ''			    
            sleep 3
	    echo ''
	    if [ -e $gitorious_dir/script/restore ]; then
	            echo 'Running restore script'
		    sleep 3
		    chmod +x $gitorious_dir/script/restore && $gitorious_dir/script/restore $snapshot_dir
	    else 
		    echo 'Copying restore script'
		cp $scr_dir/gitorious_scripts/restore $gitorious_dir/script/ && chmod +x $gitorious_dir/script/restore && cp $scr_dir/gitorious_scripts/backup.rake $gitorious_dir/lib/task/
	        echo "Running restore script"
	        sleep 3
		$gitorious_dir/script/restore $snapshot_dir
	    fi
            break;;
        3)
	    ./skr_test.sh
	    break;;
	4)
	    exit
    esac
done
